package io.renren.modules.hotel.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.app.dto.HotelQueryDto;
import io.renren.modules.hotel.config.PageRequestDto;
import io.renren.modules.hotel.dao.HotelQueryDao;
import io.renren.modules.hotel.dto.frequentlyaskedquestionEntityDto;
import io.renren.modules.hotel.entity.HotelMemberLevelDetailEntity;
import io.renren.modules.hotel.entity.frequentlyaskedquestionEntity;
import io.renren.modules.hotel.service.HotelQueryService;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 雪落
 * @date 2021/2/21 - 22:44
 */
@Service
public class HotelQueryServiceImpl extends ServiceImpl<HotelQueryDao, frequentlyaskedquestionEntity> implements HotelQueryService {
    /**
     * 分页查询列表
     *
     * @param dto 查询条件
     */
    @Override
    public String selectbylist(frequentlyaskedquestionEntityDto dto) {
        //1 参数检查
        if (dto == null) {
            try {
                throw new Exception("分页查询列表参数有误");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 检查分页参数
        dto.checkParam();
        // 构建分页对象
        IPage<frequentlyaskedquestionEntity> page = new Page<>(dto.getPage(), dto.getSize());

        // 构建条件查询对象
        LambdaQueryWrapper<frequentlyaskedquestionEntity> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getOverview())) {
            wrapper.like(frequentlyaskedquestionEntity::getOverview, dto.getOverview());
        } else if (dto.getEncoding() != null) {
            wrapper.eq(frequentlyaskedquestionEntity::getEncoding, dto.getEncoding());
        }
        // 按照ord 排序 升序
        wrapper.orderByAsc(frequentlyaskedquestionEntity::getEncoding);
        IPage<frequentlyaskedquestionEntity> pageResult = page(page, wrapper);
        //封装到set里返回
        HashSet<Object> hashSet = new HashSet<>();
        hashSet.add(dto.getPage());
        hashSet.add(dto.getSize());
        hashSet.add((int) pageResult.getTotal());
        hashSet.add(pageResult.getRecords());
        String jsonString = JSON.toJSONString(hashSet);
        return jsonString;
    }

    @Override
    public String add(frequentlyaskedquestionEntity dto) {
        if (StringUtils.isNotBlank(dto.getOverview()) && dto.getEncoding() != null) {
            save(dto);

        } else {
            try {
                throw new Exception("新增参数有误");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "新增成功";
    }

    @Override
    public String del(HotelQueryDto dto) {
        if (dto != null||dto.getIds().size()!=0) {
            removeByIds(dto.getIds());
        }
        return "删除成功";
    }

    @Override
    public String upd(frequentlyaskedquestionEntity dto) {
        //1 参数检查
        if (dto == null) {
            try {
                throw new Exception("更新参数有误");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        updateById(dto);
        return "更新成功";
    }
}
